#导入包
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#定义一个邮件发送类
class EmailSend:
    #初始化self信息
    def __init__(self,from_addrs = "Lucas127i@qq.com",password = "mvbjjczejanibaea",to_addr = "3468718331@qq.com",smtp_server = "smtp.qq.com",subject = None):
        self.from_addrs = from_addrs
        self.password = password
        self.to_addr = to_addr
        self.smtp_server = smtp_server
        self.subject = subject
    #声明一个定义msg的方法
    def define_msg(self,Text = None,_subtype = input("你要发送什么格式的邮件,plain还是html:"),_charset = "utf-8",msg = None):
        if _subtype == 'html':
            html_file = """
            <a href = "https://www.baidu.com">百度</a>
            """
            self.Text = html_file
        else:
            self.Text = input("正文内容:")
        self._subtype = _subtype
        self._charset = _charset
        self.msg = MIMEText(self.Text,_subtype = self._subtype,_charset = self._charset)
    #声明一个头文件信息方法
    def HeadMessage(self):
        self.define_msg()
        self.msg["From"] = Header(self.from_addrs)
        self.msg["To"] = Header(self.to_addr)
        subject = "Python发送邮件"#主题
        self.msg["Subject"] = Header(subject)


    #声明发送方法
    def send(self):
        self.HeadMessage()
        try:
            smtp = smtplib.SMTP_SSL(self.smtp_server, port=465)
            smtp.login(self.from_addrs, self.password)
            smtp.sendmail(self.from_addrs, self.to_addr, self.msg.as_string())
            print("邮件发送成功")
        except Exception as e:
            print('邮件发送失败！', e)
    #声明一个html信息发送方法
if __name__ == "__main__":
    da = EmailSend()
    da.send()